<template>
  <el-table
      :data="tableRowLists"
      :span-method="mergeHanlder"
      border
      header-row-class-name="test"
      style="width: 100%">
    <el-table-column align="center" label="国家">
      <template #default="{row:{campaignIndex}}">
        {{ tableData[campaignIndex].country_name }}
      </template>
    </el-table-column>
    <el-table-column align="center" label="省份">
      <template #default="{row:{campaignIndex,unitIndex,customIndex}}">
        {{ tableData[campaignIndex].province[unitIndex].province_name }}
      </template>
    </el-table-column>
    <el-table-column
        label-class-name="aa"
        align="center" label="市">
      <el-table-column
          v-for="(item, index) of city"
          :key="item"
          label-class-name="hide"
          align="center"
          :label="item + ''" min-width="60">
        <template #default="{row:{campaignIndex,unitIndex,customIndex}}">
          <el-input v-if="tableData[campaignIndex].province[unitIndex].city_list[index]?.city_id"
                    v-model="tableData[campaignIndex].province[unitIndex].city_list[index].city_id"/>
          {{ tableData[campaignIndex].province[unitIndex].city_list[index]?.city_name }}
        </template>
      </el-table-column>
    </el-table-column>
  </el-table>
</template>

<script>
import {
  computed,
  reactive,
  toRefs,
} from 'vue'


function findLongestCityList(data) {
  let maxLength = 0;
  let longestCityList = [];

  for (const country of data) {
    for (const province of country.province) {
      if (province.city_list.length > maxLength) {
        maxLength = province.city_list.length;
        longestCityList = province.city_list;
      }
    }
  }

  return longestCityList;
}

export default {
  name: "DetailsTable",
  props: {
    rowDataLists: {
      type: Array,
      default: [],
    },
    data: {
      type: Object,
      default: {}
    },
  },
  emits: ['rowMerge', 'delTask'],
  setup(props, {emit}) {

    const tableData = computed(() => props.data)

    const city = computed(() => {
      return findLongestCityList(props.data).length
    })
    const tableRowLists = computed(() => props.rowDataLists)
    const mergeHanlder = ({row, column, rowIndex, columnIndex}) => {
      return tableRowLists.value[rowIndex][columnIndex] || [1, 1]
    }

    requestIdleCallback(() => {
      document.querySelector('.hide').parentNode.style.display = 'none'
      // console.log(document.querySelectorAll('.test'))
    }, {
      timeout: 0
    })

    return {
      city,
      mergeHanlder,
      tableData,
      tableRowLists
    }
  }
}
</script>
<style>
.hide {
  display: none;
}
</style>
